package com.caimiao.miao.page.master.storage.enter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;

import com.caimiao.miao.R;
import com.caimiao.miao.interfaces.BaseFragmentImpl;
import com.caimiao.miao.service.MiaoService;
import com.caimiao.miao.service.MiaoServiceConnection;
import com.caimiao.miao.widget.ISpinner;

public class EnterFragment extends BaseFragmentImpl<IEnterContact.Presenter> implements IEnterContact.View, View.OnClickListener {
    private ISpinner mSpType;
    private EditText mEtCount;
    private MiaoServiceConnection conn;

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_storage_enter;
    }


    @Override
    public void initUI() {
        setTitle(R.string.title_storage);
        mSpType = mRootView.findViewById(R.id.storageEnterSpType);
        mEtCount = mRootView.findViewById(R.id.storageEnterEtCount);
    }

    @Override
    public void initLogic() {
        mRootView.findViewById(R.id.storageEnterBtnSave).setOnClickListener(this::onClick);
        new EnterPresenter(this);

        Intent intent = new Intent(getContext(), MiaoService.class);
        conn = new MiaoServiceConnection(mPresenter);
        getContext().bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        mSpType.setAdapter(adapter);
    }

    @Override
    public String getType() {
        return mSpType.getText().toString();
    }

    @Override
    public int getCount() {
        String count = mEtCount.getText().toString();
        if (TextUtils.isEmpty(count))
            return 0;
        return Integer.valueOf(count);
    }

    @Override
    public void onDestroy() {
        getContext().unbindService(conn);
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        mPresenter.save(getContext(), getType(), getCount());
    }

    @Override
    public void close(Bundle extra) {

    }
}
